package whu.cn.whushare.ui.main.known.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.ImageWorkerManager;
import whu.cn.whushare.ui.main.known.bean.AnswerWrapBean;
import whu.cn.whushare.util.Utils;

public class AnswerListAdapter extends BaseAdapter {
	private Context mContext;
	private List<AnswerWrapBean> mAnswerDetail;

	public AnswerListAdapter(Context mContext, List<AnswerWrapBean> mAnswerDetail) {
		this.mContext = mContext;
		this.mAnswerDetail = mAnswerDetail;
	}

	@Override
	public int getCount() {
		return mAnswerDetail.size();
	}

	@Override
	public AnswerWrapBean getItem(int position) {
		return mAnswerDetail.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_answer_all_item, null);
			viewHolder = new ViewHolder();
			ViewMappingUtil.mapView(viewHolder, convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		AnswerWrapBean all_answer = mAnswerDetail.get(position);
		viewHolder.agree_count.setText(all_answer.answer.getVoteCount() + "");
        viewHolder.answer_content.setText(all_answer.answer.answerContext+"");
        final String html=all_answer.answer.getAnswerContext()+"";


        new AsyncTask<String,String,Spanned>(){
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Spanned s) {
                super.onPostExecute(s);

                viewHolder.answer_content.setText(s);

            }

            @Override
            protected Spanned doInBackground(String... params) {
                Spanned sp = Html.fromHtml(html,
                        new Html.ImageGetter() {
                            @Override
                            public Drawable getDrawable(String source) {
                                source = "http://www.027team.com" + source;
                                InputStream is = null;
                                try {
                                    is = new URL(source).openStream();
                                    Drawable d = Drawable.createFromStream(is, "src");
                                    d.setBounds(0, 0, d.getIntrinsicWidth()*3, d.getIntrinsicHeight()*3);
                                    is.close();
                                    return d;
                                } catch (Exception e) {
                                    return null;
                                }
                            }
                        }, null);
                return sp;
            }
        }.execute();

		User user = all_answer.user;
		viewHolder.answerName.setText(user.getNickName() + "");
		if (!TextUtils.isEmpty(user.getAvatar())) {
			ImageWorkerManager.getInstance(mContext).loadCommonImage(Utils.getPhotoUrl(user.getAvatar()),
					viewHolder.answerAvatar, R.drawable.avatar, R.drawable.avatar);
		}
		return convertView;
	}

	public class ViewHolder {
		@ViewMapping(id = R.id.answer_person)
		private ImageView answerAvatar;
		@ViewMapping(id = R.id.answer_person_nickname)
		private TextView answerName;
		@ViewMapping(id = R.id.agree_count)
		private TextView agree_count;
		@ViewMapping(id = R.id.answer_content)
		private TextView answer_content;
	}

}
